/**
 * 非常规工作日时间设置
 */
Ext.namespace("Ext.ux.workholiday");
 
Ext.ux.workholiday.SpecialTimeSetPanel = Ext.extend(Ext.Panel, { 

	border: false,
	layout: 'fit',

	grid: null,

	initComponent:function(){
		
		Ext.ux.workholiday.SpecialTimeSetPanel.superclass.initComponent.call(this);
		
		var gridStructure = [{  
			name: 'id',
			hidden: true
		},{
			name: 'day',  
			header: '日期',
			sortable: true,
			width: 100
		},{  
			name: 'holidayOrWork',  
			header: '假期或加班',
			sortable: true,
			width: 100
		},{  
			name: 'timeSpanDesc',  
			header: '时间长度',
			sortable: true,
			width: 100
		},{  
			name: 'startTime',  
			header: '起始时间',
			sortable: true,
			width: 100
		},{  
			name: 'endTime',  
			header: '结束时间',
			sortable: true,
			width: 100
		},{  
			name: 'memo',  
			header: '备注',
			width: 100
		}];
	   
		
		//非常规工作时间列表
		var specialTimeGrid = new Ext.ux.grid.CrudGrid({
			id: 'specialTimeGrid',
			structure: gridStructure,
			url: 'wh-special-time!list.action',
			defaultSortField: 'day', 
			defaultSortDir: 'desc',
			keyField: 'id',
			title: '非常规时间管理',
			showAddBtn: false,
			fTbar: ['-',{
				text: '新增非常规时间段',
				tooltip: '新增非常规时间段',
			    iconCls: 'icon-add',
			    handler: this.addSpecialPeriod,
			    scope: this
			},'-',{
				text: '新增非常规时间',
				tooltip: '新增非常规时间',
			    iconCls: 'icon-add',
			    handler: this.addSpecialDay,
			    scope: this
			}]
		});

		specialTimeGrid.on('rowdblclick', function(grid, rowIndex, e){
			var sel = grid.getSelectionModel().getSelections();
			var id = sel[0].get('id');
			this.loadById(id);
		}, this);
		
		specialTimeGrid.on('updateRecord', function(id){
			this.loadById(id);
		}, this);
		
		specialTimeGrid.on('deleteRecord', function(grid, ids){
			this.deleteRecord(ids);
		}, this);
		
		this.add(specialTimeGrid);
		this.grid = specialTimeGrid;
	},
	
	//添加非常规工作时间段
	addSpecialPeriod: function(){
		this.getSpecialPeriodDetailWindow().show();
	},
		
	//添加一条非常规时间记录
	addSpecialDay: function(){
		var specialDayDetailWindow = this.getSpecialDayDetailWindow();
		specialDayDetailWindow.clearForm();
		specialDayDetailWindow.setDefaultDay();
		specialDayDetailWindow.show();
	},
		
	//删除一条或多条非常规时间列表记录
	deleteRecord: function(ids){
			Ext.Ajax.request({
				url: 'wh-special-time!delete.action',
				success: function(responese){
					this.grid.store.reload();
					Ext.Msg.alert('系统提示', '已成功删除选中的附件记录！');
				},
				failure: function(){
					Ext.Msg.alert('系统提示', '删除失败');
				},
				method: 'POST',
				params:{
					ids: ids
				},
				scope: this
			});
	},
	
	//加载一条非常规时间列表记录
	loadById: function(id){
			var specialDayDetailWindow = Ext.getCmp('specialDayDetailWindow');
			if(specialDayDetailWindow == null){
				specialDayDetailWindow= new Ext.ux.workholiday.SpecialDayDetailWindow({
					id: 'specialDayDetailWindow',
					title:'修改非常规时间'
				});
			}
			specialDayDetailWindow.loadById(id);
			specialDayDetailWindow.show();
	},
	
	getSpecialPeriodDetailWindow: function(){
		var specialPeriodDetailWindow = Ext.getCmp('specialPeriodDetailWindow');
		if(specialPeriodDetailWindow == null){
			specialPeriodDetailWindow= new Ext.ux.workholiday.SpecialPeriodDetailWindow({
				id: 'specialPeriodDetailWindow',
				title:'新增非常规时间段'
			});
		}
		return specialPeriodDetailWindow;
	},
	
	getSpecialDayDetailWindow: function(){
		var specialDayDetailWindow = Ext.getCmp('specialDayDetailWindow');
			if(specialDayDetailWindow == null){
				specialDayDetailWindow= new Ext.ux.workholiday.SpecialDayDetailWindow({
					id: 'specialDayDetailWindow',
					title:'新增非常规时间'
				});
		}
		return specialDayDetailWindow;
	}
});